package top.zhangjianyong.test.splitwords;

import cn.hutool.crypto.digest.MD5;
import org.apache.commons.io.IOUtils;
import top.zhangjianyong.entity.Word;
import top.zhangjianyong.utils.ArgsUtil;
import top.zhangjianyong.util.JDBCUtil;
import top.zhangjianyong.utils.SplitWordUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2022/8/19 5:09 PM
 */
public class SplitWords {


    private static final String UNUSED_WORDS = " \t\r\n、，。：；“‘”【】『』|=+-——（）*&……%￥#@！~·《》？/?<>,.;:'\"[]{}_)(^$!`";
    private static final String UNUSED_ENGLISH = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    public static void splitWords(String[] args, String fileName, String source) throws SQLException {
        Map<String, String> map = ArgsUtil.convertMap(args, "-D");
        String host = map.get("MYSQL.HOST");
        String port = map.get("MYSQL.PORT");
        String username = map.get("MYSQL.USER");
        String password = map.get("MYSQL.PSWD");

        System.out.println("清洗数据...");
        source = SplitWordUtil.clean(source);
        String md5 = MD5.create().digestHex(source);
        System.out.println("分词...");
        Map<String, Word> wordsMap = SplitWordUtil.splitWords(source, 4);
        System.out.println("计算凝固度自由度...");
        SplitWordUtil.stoneAndFree(wordsMap);

        System.out.println("筛选数据...");
        List<Word> collect = wordsMap.values().stream()
                .filter(word ->
                        word.getCount() > 10 && word.getValue().length() > 1
                                && word.getFree() > 0.5 && word.getStone() > 50
                )
                .collect(Collectors.toList());
        System.out.println("数据入库...");
        Connection connection = JDBCUtil.getConnection(host, port, username, password);
        connection.setAutoCommit(false);

        System.out.println("删除重复数据...");
        PreparedStatement delPs = connection.prepareStatement("delete from dictionary where md5=?");
        delPs.setString(1, md5);
        delPs.executeUpdate();
        connection.commit();
        int size = collect.size();
        System.out.println("插入数据...");
        System.out.println("数量：" + size);
        String sql = "INSERT INTO dictionary (word,count,frequency,stone,free,book,md5) VALUES(?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        int part = size / 100;
        part = part == 0 ? 1 : part;
        for (int i = 0; i < size; i++) {
            Word word = collect.get(i);
            preparedStatement.setString(1, word.getValue());
            preparedStatement.setInt(2, word.getCount());
            preparedStatement.setDouble(3, word.getFrequency());
            preparedStatement.setDouble(4, word.getStone());
            preparedStatement.setDouble(5, word.getFree());
            preparedStatement.setString(6, fileName);
            preparedStatement.setString(7, md5);
            preparedStatement.addBatch();
            if (i % part == part - 1) {
                preparedStatement.executeBatch();
                connection.commit();
                System.out.println("完成 :" + ((double) i / size * 100) + "%");
            }
        }

        if (size % part != 0) {
            preparedStatement.executeBatch();
            connection.commit();
        }
        System.out.println("入库完成...");
    }

    public static void main(String[] args) throws IOException, SQLException {
        String path = "/Users/zhangjianyong/IdeaProjects/SmallTools/src/main/resources/";
        String fileName = "西游记.txt";
        //String fileName = "诛仙.txt";

        System.out.println("分析文本：" + fileName);
        File file = new File(path + fileName);
        String source = IOUtils.toString(new FileInputStream(file), Charset.defaultCharset());
        splitWords(args, fileName, source);

    }
}
